#! /bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
#--------------------------------------------------------------------------- #


if [ -z "$ONE_LOCATION" ] ; then
    echo "Please define ONE_LOCATION first. Exiting..."
    exit 1
fi

ONE_LIB_DIR=$ONE_LOCATION/lib
ONE_ETC_DIR=$ONE_LOCATION/etc
ONE_VAR_DIR=$ONE_LOCATION/var
ONE_SHARE_DIR=$ONE_LOCATION/share

ONE_VMM_DIR_VBOX=remotes/vmm/vbox
ONE_IM_DIR_VBOX=remotes/im/vbox.d
EXAMPLES_VBOX=examples/vbox
ONE_ETC_DIR_IM_VBOX=$ONE_ETC_DIR/im_vbox
ONE_ETC_DIR_VMM_SSH_FILE=$ONE_ETC_DIR/vmm_ssh/vmm_ssh_vbox.conf

# copy remotes in lib/
[[ -d $ONE_LIB_DIR/$ONE_VMM_DIR_VBOX ]] || mkdir -p $ONE_LIB_DIR/$ONE_VMM_DIR_VBOX
cp -v $ONE_VMM_DIR_VBOX/* $ONE_LIB_DIR/$ONE_VMM_DIR_VBOX
[[ -d $ONE_LIB_DIR/$ONE_IM_DIR_VBOX ]] || mkdir -p $ONE_LIB_DIR/$ONE_IM_DIR_VBOX
cp -v $ONE_IM_DIR_VBOX/* $ONE_LIB_DIR/$ONE_IM_DIR_VBOX

#copy remotes in var/
[[ -d $ONE_VAR_DIR/$ONE_VMM_DIR_VBOX ]] || mkdir -p $ONE_VAR_DIR/$ONE_VMM_DIR_VBOX
cp -v $ONE_VMM_DIR_VBOX/* $ONE_VAR_DIR/$ONE_VMM_DIR_VBOX
[[ -d $ONE_VAR_DIR/$ONE_IM_DIR_VBOX ]] || mkdir -p $ONE_VAR_DIR/$ONE_IM_DIR_VBOX
cp -v $ONE_IM_DIR_VBOX/* $ONE_VAR_DIR/$ONE_IM_DIR_VBOX

# prepare im_vbox dir in etc
[[ -d $ONE_ETC_DIR_IM_VBOX ]] || mkdir -p $ONE_ETC_DIR_IM_VBOX

# copy vmm_ssh_vbox.conf file with default attributes vbox driver
cp -v etc/vmm_ssh/vmm_ssh_vbox.conf $ONE_ETC_DIR_VMM_SSH_FILE	

# copy sunstone plugin
cp -v sunstone/vbox-plugin.js $ONE_LIB_DIR/sunstone/public/js/plugins/

# patch index.html to load plugin
patch -p0 -t $ONE_LIB_DIR/sunstone/templates/index.html < sunstone/index.html.patch 

# install examples
[[ -d $ONE_SHARE_DIR/$EXAMPLES_VBOX ]] || mkdir -p $ONE_SHARE_DIR/$EXAMPLES_VBOX
cp -v share/$EXAMPLES_VBOX/* $ONE_SHARE_DIR/$EXAMPLES_VBOX
echo "Installation finished successfully!"
